<template>
	<view class="content">
		<view>
			<form @submit="formSubmit" @reset="formReset" class="registFrom">
				<view class="uni-form-item uni-column">
					<view class="title">用户名:</view>
					<input class="input" name="userName" placeholder="请输入您的用户名" />
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">密码:</view>
					<input class="input" name="password" type="password" placeholder="请输入您的密码" />
				</view>

				<view class="uni-form-item uni-column">
					<view class="title">联系方式:</view>
					<input class="input" name="phone" placeholder="请输入您的联系方式" />
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">邮箱:</view>
					<input class="input" name="email" placeholder="请输入您的邮箱" />
				</view>
				<view class="uni-btn-v">
					<button form-type="submit" class="btn regist">注册</button>
					<button type="default" form-type="reset" class="btn reset">重置表单</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		methods: {
			formSubmit: function(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				var formdata = e.detail.value
				/* uni.showModal({
				    content: '表单数据内容：' + JSON.stringify(formdata),
				    showCancel: false
				}); */
				uni.request({
					header: {
						'content-type': 'application/json', //如果后台使用 @Reuqestbody的，使用这种方式
					},
					url: "http://127.0.0.1:8090/user/saveUser",
					method: "POST",
					data: formdata,
					dataType: "json",
					timeout: 60000,
					success: function(res) {
						console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								title: "注册成功",
								icon: "none",
								mask: true,
								position: "top"
							});
							setTimeout(function() {
								uni.redirectTo({
									url: "./login"
								});
							}, 2000);

						} else {
							uni.showToast({
								title: "注册失败",
								icon: "none",
								mask: true,
								position: "top"
							});
						}
					}
				})
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.uni-column {
		display: flex;
		justify-content: space-around;
		align-items: center;
		margin-top: 10px;
	}

	.content {}

	.title {
		width: 80px;
		text-align: right;
	}

	.registFrom {
		width: 100%;
	}

	.input {
		border: 1rpx solid #eee;
		/* //border-radius: 10rpx; */
		border-radius: 20px;
		text-align: center;
		font-size: 15px;
	}

	.uni-btn-v {
		display: flex;
		justify-content: space-around;
		align-items: center;

	}

	.btn .regist {
		size: 'mini';
		plain: true;
	}
</style>
